package ca.etsmtl.log320;

public class Board {
	
	
	private static final int BOARD_BLANC = 0;
	private static final int BOARD_NOIR = 1;
	
	long[] mBoard = new long[2];
	
	public Board() {
	}
	public Board(int[][] board) {

		for (int j=0; j<8; j++) {
			for(int i=0; i<8; i++) {
				mBoard[BOARD_BLANC] <<= 1;
				mBoard[BOARD_NOIR] <<= 1;
				
				switch(board[i][j]) {
					case Consts.CASE_BLANC:
						mBoard[BOARD_BLANC] |= 1;
					case Consts.CASE_NOIR:
						mBoard[BOARD_NOIR] |= 1;
				}
			}
		}
	}

	public long[] get() {
		return mBoard;
	}
	
	public int get(int x, int y) {
		if((mBoard[BOARD_BLANC] & (1 << (((7-y)<<3) + (7-x)))) != 0)//x*8 + y
			return Consts.CASE_BLANC;
		if((mBoard[BOARD_NOIR] & (1 << (((7-y)<<3) + (7-x)))) != 0)//x*8 + y
			return Consts.CASE_NOIR;
		return Consts.CASE_VIDE;
	}
	
	public static int get(long[] bitBoard, int x, int y) {
		long toTest = ((long)1 << (((7-y)<<3) + (7-x)));
		if((bitBoard[BOARD_BLANC] & toTest) != 0)//x*8 + y
			return Consts.CASE_BLANC;
		if((bitBoard[BOARD_NOIR] & toTest) != 0)//x*8 + y
			return Consts.CASE_NOIR;
		return Consts.CASE_VIDE;
	}
	
	public static void set(long[] bitBoard, int x, int y, int color) {
		long toSet = ((long)1 << (((7-y)<<3) + (7-x)));
		switch(color) {
		case Consts.CASE_BLANC:
			bitBoard[BOARD_NOIR] &= ~toSet;
			bitBoard[BOARD_BLANC] |= toSet;
			break;
		case Consts.CASE_NOIR:
			bitBoard[BOARD_BLANC] &= ~toSet;
			bitBoard[BOARD_NOIR] |= toSet;
			break;
		case Consts.CASE_VIDE:
			bitBoard[BOARD_BLANC] &= ~toSet;
			bitBoard[BOARD_NOIR] &= ~toSet;
			break;
		}
	}
}
